#ifndef _DRIVER_ADC_H_
#define _DRIVER_ADC_H_ 

  #include <MK40DZ10.h>
  #include "PounceTypes_v2.h"
    
  /**
    Global Defines 
  */
  
  // ADCSC1
  
  #define A                   0x0
  #define B                   0x1
  
  #define COCO_COMPLETE       ADC_SC1_COCO_MASK
  #define COCO_NOT            0x00
  
  #define AIEN_ON             ADC_SC1_AIEN_MASK
  #define AIEN_OFF            0x00
  
  #define DIFF_SINGLE         0x00
  #define DIFF_DIFFERENTIAL   ADC_SC1_DIFF_MASK
  
  // ADCCFG1

  #define ADLPC_LOW           ADC_CFG1_ADLPC_MASK
  #define ADLPC_NORMAL        0x00
  
  #define ADIV_1              0x00
  #define ADIV_2              0x01
  #define ADIV_4              0x02
  #define ADIV_8              0x03
  
  #define ADLSMP_LONG         ADC_CFG1_ADLSMP_MASK
  #define ADLSMP_SHORT        0x00
  
  #define MODE_8              0x00
  #define MODE_12             0x01
  #define MODE_10             0x02
  #define MODE_16             0x03
  
  #define ADICLK_BUS          0x00
  #define ADICLK_BUS_2        0x01
  #define ADICLK_ALTCLK       0x02
  #define ADICLK_ADACK        0x03
  
  // ADCCFG2
  
  #define ADCB_SEL            ADC_CFG2_MUXSEL_MASK
  #define ADCA_SEL            0x00
  
  #define ADACKEN_ENABLED     ADC_CFG2_ADACKEN_MASK
  #define ADACKEN_DISABLED    0x00
  
  #define ADHSC_HISPEED       ADC_CFG2_ADHSC_MASK
  #define ADHSC_NORMAL        0x00
  
  #define ADLSTS_20           0x00
  #define ADLSTS_12           0x01
  #define ADLSTS_6            0x02
  #define ADLSTS_2            0x03
  
  //ADCSC2

  #define ADACT_ACTIVE        ADC_SC2_ADACT_MASK
  #define ADACT_INACTIVE      0x00
  
  #define ADTRG_HW            ADC_SC2_ADTRG_MASK
  #define ADTRG_SW            0x00
  
  #define ACFE_DISABLED       0x00
  #define ACFE_ENABLED        ADC_SC2_ACFE_MASK
  
  #define ACFGT_GREATER       ADC_SC2_ACFGT_MASK
  #define ACFGT_LESS          0x00
  
  #define ACREN_ENABLED       ADC_SC2_ACREN_MASK
  #define ACREN_DISABLED      0x00
  
  #define DMAEN_ENABLED       ADC_SC2_DMAEN_MASK
  #define DMAEN_DISABLED      0x00
  
  #define REFSEL_EXT          0x00
  #define REFSEL_ALT          0x01
  #define REFSEL_RES          0x02
  #define REFSEL_RES_EXT      0x03     /* reserved but defaults to Vref */
  
  //ADCSC3

  #define CAL_BEGIN           ADC_SC3_CAL_MASK
  #define CAL_OFF             0x00
  
  #define CALF_FAIL           ADC_SC3_CALF_MASK
  #define CALF_NORMAL         0x00
  
  #define ADCO_CONTINUOUS     ADC_SC3_ADCO_MASK
  #define ADCO_SINGLE         0x00
  
  #define AVGE_ENABLED        ADC_SC3_AVGE_MASK
  #define AVGE_DISABLED       0x00
  
  #define AVGS_4              0x00
  #define AVGS_8              0x01
  #define AVGS_16             0x02
  #define AVGS_32             0x03
  
  //PGA
  
  #define PGAEN_ENABLED       ADC_PGA_PGAEN_MASK
  #define PGAEN_DISABLED      0x00
  
  #define PGACHP_CHOP         ADC_PGA_PGACHP_MASK
  #define PGACHP_NOCHOP       0x00
  
  #define PGALP_LOW           ADC_PGA_PGALP_MASK
  #define PGALP_NORMAL        0x00
  
  #define PGAG_1              0x00
  #define PGAG_2              0x01
  #define PGAG_4              0x02
  #define PGAG_8              0x03
  #define PGAG_16             0x04
  #define PGAG_32             0x05
  #define PGAG_64             0x06
  
  #define CAL_BLK_NUMREC      0x12

  #define CHANNEL_A           0x00
  #define CHANNEL_B           0x01                   
  
  /**
    ADC Data Structures
  */
  typedef struct adc_cfg 
  {
    U08 CONFIG1;
    U08 CONFIG2;
    U16 COMPARE1;
    U16 COMPARE2;
    U08 STATUS2;
    U08 STATUS3;
    U08 STATUS1A;
    U08 STATUS1B;
    U32 PGA;
  }*tADC_ConfigPtr, tADC_Config;
  
  typedef struct adc_cal 
  {
    U16 OFS;
    U16 PG;
    U16 MG;
    U08 CLPD;
    U08 CLPS;
    U16 CLP4;
    U16 CLP3;
    U08 CLP2;
    U08 CLP1;
    U08 CLP0;
    U08 dummy;
    U08 CLMD;
    U08 CLMS;
    U16 CLM4;
    U16 CLM3;
    U08 CLM2;
    U08 CLM1;
    U08 CLM0;
  }tADC_Cal_Blk ;
  
  /**
    Public Function Prototypes
  */
    
  void _ADC0_Init(U08 ADC_Channel, U08 Channel_A_B, U08 Mode);
  void _ADC0_Disable(void);
  U16 _ADC0_Read(U08 ADC_Channel, U08 Channel_A_B);

  void _ADC1_Init(U08 ADC_Channel, U08 Channel_A_B, U08 Mode);
  void _ADC1_Disable(void);
  U16 _ADC1_Read(U08 ADC_Channel, U08 Channel_A_B);  

#endif /* __DRIVER_ADC_H__ */